const amqp = require('amqplib');
const config = require('../config/dev')
async function product() {
    // 1. 创建链接对象
    const connection = await amqp.connect(config.rabbitmq);
    // 2. 获取通道
    const channel = await connection.createChannel();
    // 3. 声明参数
    const routingKey = 'statis-task';
    // 如果没有channel.assertQueue 消息在queue中看不到
    // durable 为true 就是server重启列队不会消失，（是否持久化）
    // exclusive 只可以被一个消费者消费
    await channel.assertQueue(routingKey, {durable: true, exclusive: false});

    const msg = {
        action: 'cutTaskStatisConsumer',
        subjectId: 11720681,
        userId: '12312312312321312',
        event: 'recycleTask',
        uploadOriginPaperTime: new Date(),
        startCutTime: new Date(new Date().getTime() + 1000 * 60 * 60 * 2),
        userGetTaskTime: new Date(new Date().getTime() + 1000 * 60 * 60 * 1),
        create_time: new Date().getTime(),
        role: 'CUSTOM_SERVICE'
    };
    for (let i = 0; i < 2; i++) {
        // 4. 发送消息
        await channel.publish('', routingKey,Buffer.from(JSON.stringify(msg)),(err,res)=>{
            if (err) console.log(err);
            console.log(res)
        });
    }
    // 5. 关闭通道
    await channel.close();
    // 6. 关闭连接
    await connection.close();
}

product();
